/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-07-26
*/
package com.rzico.order.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Product;
import com.rzico.basics.service.ProductService;
import com.rzico.exception.CustomException;
import com.rzico.order.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.order.mapper.EmptyBarrelMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 空桶记录表业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class EmptyBarrelService extends BaseServiceImpl<EmptyBarrel, String> {

    @Autowired
    private EmptyBarrelMapper emptyBarrelMapper;

    @Autowired
    private ProductService productService;

    @Autowired
    private ReturnItemService returnItemService;

    @Override
    public BaseMapper<EmptyBarrel, String> getMapper() {
        return emptyBarrelMapper;
    }

    public List<EmptyBarrel> getItemList(Long shippingId) {
        Map<String,Object> params = new HashMap<>();
        params.put("shippingId",shippingId);
        return emptyBarrelMapper.selectList(params);
    }

    public BigDecimal selectQuantitySum(Long memberId) {
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",memberId);
        return emptyBarrelMapper.selectQuantitySum(params);
    }


    //送货
    @Transactional(rollbackFor = Exception.class)
    public void addShipping(Shipping shipping) {
        for (ShippingItem shippingItem:shipping.getItemList()) {
            if (shippingItem.getSubType().equals(4)) {
                EmptyBarrel emptyBarrel = new EmptyBarrel();
                emptyBarrel.setAdminFreight(shippingItem.getAdminFreight());
                emptyBarrel.setShippingFreight(shippingItem.getShippingFreight());
                emptyBarrel.setLevelFreight(shippingItem.getLevelFreight());
                emptyBarrel.setCost(shippingItem.getCost());
                emptyBarrel.setProductId(shippingItem.getProductId());
                emptyBarrel.setEmptyType(1);
                emptyBarrel.setCreateDate(new Date());
                emptyBarrel.setEnterpriseId(shipping.getEnterpriseId());
                emptyBarrel.setMemberId(shipping.getMemberId());
                emptyBarrel.setName(shippingItem.getName());
                emptyBarrel.setSpec(shippingItem.getSpec());
                emptyBarrel.setUnit(shippingItem.getUnit());
                emptyBarrel.setThumbnail(shippingItem.getThumbnail());
                emptyBarrel.setSn(shippingItem.getSn());
                emptyBarrel.setShippingId(shipping.getId());
                emptyBarrel.setOrderId(shipping.getOrderId());
                emptyBarrel.setOrderSn(shipping.getOrderSn());
                emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(shippingItem.getQuantity()));
                emptyBarrel.setShopId(shipping.getShopId());
                emptyBarrel.setShippingEmployeeId(shipping.getShippingEmployeeId());
                emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);

                if (shippingItem.getEmptyBarrelQuantity().compareTo(BigDecimal.ZERO)>0) {
                    emptyBarrel.setAdminFreight(shippingItem.getAdminFreight());
                    emptyBarrel.setShippingFreight(shippingItem.getShippingFreight());
                    emptyBarrel.setLevelFreight(shippingItem.getLevelFreight());
                    emptyBarrel.setCost(shippingItem.getCost());
                    emptyBarrel.setProductId(shippingItem.getProductId());
                    emptyBarrel.setEmptyType(2);
                    emptyBarrel.setCreateDate(new Date());
                    emptyBarrel.setEnterpriseId(shipping.getEnterpriseId());
                    emptyBarrel.setMemberId(shipping.getMemberId());
                    emptyBarrel.setName(shippingItem.getName());
                    emptyBarrel.setSpec(shippingItem.getSpec());
                    emptyBarrel.setUnit(shippingItem.getUnit());
                    emptyBarrel.setThumbnail(shippingItem.getThumbnail());
                    emptyBarrel.setSn(shippingItem.getSn());
                    emptyBarrel.setShippingId(shipping.getId());
                    emptyBarrel.setOrderId(shipping.getOrderId());
                    emptyBarrel.setOrderSn(shipping.getOrderSn());
                    emptyBarrel.setQuantity(shippingItem.getEmptyBarrelQuantity());
                    emptyBarrel.setShopId(shipping.getShopId());
                    emptyBarrel.setShippingEmployeeId(shipping.getShippingEmployeeId());
                    emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);

                }
            }
        }

    }

    //回桶登记
    @Transactional(rollbackFor = Exception.class)
    public void addEmptyBarrel(List<EmptyBarrel> itemList) {
        for (EmptyBarrel emptyBarrel:itemList) {
            Product product = productService.selectByPrimaryKey(emptyBarrel.getProductId());
            emptyBarrel.setAdminFreight(BigDecimal.ZERO);
            emptyBarrel.setShippingFreight(BigDecimal.ZERO);
            emptyBarrel.setLevelFreight(BigDecimal.ZERO);
            emptyBarrel.setCost(BigDecimal.ZERO);
            emptyBarrel.setEmptyType(1);
            emptyBarrel.setCreateDate(new Date());
            emptyBarrel.setName(product.getName());
            emptyBarrel.setSpec(product.getSpec());
            emptyBarrel.setUnit(product.getUnit());
            emptyBarrel.setThumbnail(product.getThumbnail());
            emptyBarrel.setSn(product.getSn());
            emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);
        }

    }

    //出桶登记
    @Transactional(rollbackFor = Exception.class)
    public void decEmptyBarrel(List<EmptyBarrel> itemList) {
        for (EmptyBarrel emptyBarrel:itemList) {
            Product product = productService.selectByPrimaryKey(emptyBarrel.getProductId());
            emptyBarrel.setAdminFreight(BigDecimal.ZERO);
            emptyBarrel.setShippingFreight(BigDecimal.ZERO);
            emptyBarrel.setLevelFreight(BigDecimal.ZERO);
            emptyBarrel.setCost(BigDecimal.ZERO);
            emptyBarrel.setEmptyType(2);
            emptyBarrel.setCreateDate(new Date());
            emptyBarrel.setName(product.getName());
            emptyBarrel.setSpec(product.getSpec());
            emptyBarrel.setUnit(product.getUnit());
            emptyBarrel.setThumbnail(product.getThumbnail());
            emptyBarrel.setSn(product.getSn());
            emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);
        }

    }

    //退货
    @Transactional(rollbackFor = Exception.class)
    public void addReturn(Return returns) {
        for (ReturnItem returnItem:returns.getItemList()) {
            if (returnItem.getSubType().equals(4)) {
                EmptyBarrel emptyBarrel = new EmptyBarrel();
                emptyBarrel.setAdminFreight(returnItem.getAdminFreight());
                emptyBarrel.setShippingFreight(returnItem.getShippingFreight());
                emptyBarrel.setLevelFreight(returnItem.getLevelFreight());
                emptyBarrel.setCost(returnItem.getCost());
                emptyBarrel.setProductId(returnItem.getProductId());
                emptyBarrel.setEmptyType(2);
                emptyBarrel.setCreateDate(new Date());
                emptyBarrel.setEnterpriseId(returns.getEnterpriseId());
                emptyBarrel.setMemberId(returns.getMemberId());
                emptyBarrel.setName(returnItem.getName());
                emptyBarrel.setSpec(returnItem.getSpec());
                emptyBarrel.setUnit(returnItem.getUnit());
                emptyBarrel.setThumbnail(returnItem.getThumbnail());
                emptyBarrel.setSn(returnItem.getSn());
                emptyBarrel.setReturnId(returns.getId());
                emptyBarrel.setOrderId(returns.getOrderId());
                emptyBarrel.setOrderSn(returns.getOrderSn());
                emptyBarrel.setQuantity(returnItem.getQuantity());
                emptyBarrel.setShopId(returns.getShopId());
                emptyBarrel.setShippingEmployeeId(returns.getShippingEmployeeId());
                emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);

                if (returnItem.getEmptyBarrelQuantity().compareTo(BigDecimal.ZERO)>0) {
                    emptyBarrel.setAdminFreight(returnItem.getAdminFreight());
                    emptyBarrel.setShippingFreight(returnItem.getShippingFreight());
                    emptyBarrel.setLevelFreight(returnItem.getLevelFreight());
                    emptyBarrel.setCost(returnItem.getCost());
                    emptyBarrel.setProductId(returnItem.getProductId());
                    emptyBarrel.setEmptyType(1);
                    emptyBarrel.setCreateDate(new Date());
                    emptyBarrel.setEnterpriseId(returns.getEnterpriseId());
                    emptyBarrel.setMemberId(returns.getMemberId());
                    emptyBarrel.setName(returnItem.getName());
                    emptyBarrel.setSpec(returnItem.getSpec());
                    emptyBarrel.setUnit(returnItem.getUnit());
                    emptyBarrel.setThumbnail(returnItem.getThumbnail());
                    emptyBarrel.setSn(returnItem.getSn());
                    emptyBarrel.setReturnId(returns.getId());
                    emptyBarrel.setOrderId(returns.getOrderId());
                    emptyBarrel.setOrderSn(returns.getOrderSn());
                    emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(returnItem.getEmptyBarrelQuantity()));
                    emptyBarrel.setShopId(returns.getShopId());
                    emptyBarrel.setShippingEmployeeId(returns.getShippingEmployeeId());
                    emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);

                }
            }
        }

    }

}
